class Waiter {
    constructor() {
        this._data = {};
        this._tid = null;
        this._time = 600;
        this._fn = null;
        this._times = 0;
    }

    add(type, path) {
        if (!this._data[type]) {
            this._data[type] = [];
        }
        if (this._data[type].indexOf(path) === -1) {
            this._data[type].push(path);
        }
        if (this._tid !== null) {
            clearTimeout(this._tid);
        }
        this._tid = setTimeout(() => {
            this._fn && this._fn({
                add: this._data.add || [],
                remove: this._data.remove || [],
                edit: this._data.edit || []
            });
            this._data = {};
            this._tid = null;
            this._times++;
        }, this._time);
        return this;
    }

    setHandler(fn) {
        this._fn = fn;
        return this;
    }
}

module.exports = Waiter;